package com.jeesite.modules.entity;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import jakarta.validation.constraints.DecimalMin;
import com.jeesite.common.entity.DataEntity;
import com.jeesite.common.mybatis.annotation.Column;
import com.jeesite.common.mybatis.annotation.Table;

import java.io.Serial;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 支付订单实体类
 * @author LaVenderGong
 * @version 2025-08-16
 */
@Table(name="payment_orders", alias="a", label="支付订单", columns={
        @Column(name="order_id", attrName="orderId", label="订单ID", isPK=true),
        @Column(name="donation_id", attrName="donationId", label="捐赠记录ID"),
        @Column(name="payment_method", attrName="paymentMethod", label="支付方式"),
        @Column(name="amount", attrName="amount", label="支付金额"),
        @Column(name="status", attrName="status", label="订单状态"),
        @Column(name="third_party_order_id", attrName="thirdPartyOrderId", label="第三方订单号"),
        @Column(name="create_time", attrName="createTime", label="创建时间"),
        @Column(name="pay_time", attrName="payTime", label="支付时间"),
        @Column(name="callback_data", attrName="callbackData", label="回调数据"),
}, orderBy="a.create_time DESC")
public class PaymentOrder extends DataEntity<PaymentOrder> {

    @Serial
    private static final long serialVersionUID = 1L;

    private String orderId;                  // 订单ID (主键)
    private Long donationId;                 // 捐赠记录ID
    private String paymentMethod;            // 支付方式
    private BigDecimal amount;               // 支付金额
    private String status;                   // 订单状态
    private String thirdPartyOrderId;        // 第三方订单号
    private Date createTime;                 // 创建时间
    private Date payTime;                    // 支付时间
    private String callbackData;             // 回调数据

    // 订单状态常量
    public static final String STATUS_CREATED = "CREATED";     // 已创建
    public static final String STATUS_PAYING = "PAYING";      // 支付中
    public static final String STATUS_SUCCESS = "SUCCESS";    // 支付成功
    public static final String STATUS_FAILED = "FAILED";      // 支付失败
    public static final String STATUS_CANCELLED = "CANCELLED"; // 已取消
    public static final String STATUS_REFUNDED = "REFUNDED";  // 已退款

    public PaymentOrder() {
        this(null);
    }

    public PaymentOrder(String orderId) {
        super(orderId);
        this.orderId = orderId;
    }

    @NotNull(message="订单ID不能为空")
    @Size(min=1, max=64, message="订单ID长度必须在 1 到 64 个字符之间")
    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    @JsonSerialize(using = ToStringSerializer.class)
    public Long getDonationId() {
        return donationId;
    }

    public void setDonationId(Long donationId) {
        this.donationId = donationId;
    }

    @NotNull(message="支付方式不能为空")
    @Size(min=1, max=50, message="支付方式长度必须在 1 到 50 个字符之间")
    public String getPaymentMethod() {
        return paymentMethod;
    }

    public void setPaymentMethod(String paymentMethod) {
        this.paymentMethod = paymentMethod;
    }

    @NotNull(message="支付金额不能为空")
    @DecimalMin(value = "0.01", message = "支付金额必须大于0")
    public BigDecimal getAmount() {
        return amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    @NotNull(message="订单状态不能为空")
    @Size(min=1, max=20, message="订单状态长度必须在 1 到 20 个字符之间")
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Size(min=0, max=200, message="第三方订单号长度不能超过 200 个字符")
    public String getThirdPartyOrderId() {
        return thirdPartyOrderId;
    }

    public void setThirdPartyOrderId(String thirdPartyOrderId) {
        this.thirdPartyOrderId = thirdPartyOrderId;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getPayTime() {
        return payTime;
    }

    public void setPayTime(Date payTime) {
        this.payTime = payTime;
    }

    @Size(min=0, max=2000, message="回调数据长度不能超过 2000 个字符")
    public String getCallbackData() {
        return callbackData;
    }

    public void setCallbackData(String callbackData) {
        this.callbackData = callbackData;
    }

    /**
     * 检查订单是否支付成功
     */
    public boolean isSuccess() {
        return STATUS_SUCCESS.equals(this.status);
    }

    /**
     * 检查订单是否支付中
     */
    public boolean isPaying() {
        return STATUS_PAYING.equals(this.status);
    }

    /**
     * 检查订单是否已创建
     */
    public boolean isCreated() {
        return STATUS_CREATED.equals(this.status);
    }
}
